local ma__chuhai = fk.CreateSkill {
  name = "ma__chuhai",
  --[[dynamic_desc = function (self, player)
    if #player:getTableMark(self.name) == 3 then
      return "dummyskill"
    elseif player:getMark(self.name) ~= 0 then
      local str = {}
      for _, name in ipairs({"slash", "duel", "analeptic"}) do
        if table.contains(player:getMark(self.name), name) then
          table.insert(str, "<s>【"..Fk:translate(name).."】</s>")
        else
          table.insert(str, "【"..Fk:translate(name).."】")
        end
      end
      return "ma__chuhai_inner:"..table.concat(str, "")
    end
  end,]]
}

Fk:loadTranslationTable{
  ["ma__chuhai"] = "除害",
  ["@$ma__chuhai"] = "除害",
  [":ma__chuhai"] = "你可以将两张牌当无距离限制且不可被抵消的【杀】、【决斗】或【酒】使用，目标角色的濒死结算结束后你删除此牌名。<br>"..
  "当你造成或受到伤害后，若你手牌不为最多，你摸等同已删除牌名数的牌。",

  [":ma__chuhai_inner"] = "你可以将两张牌当无距离限制且不可被抵消的{1}使用，目标角色的濒死结算结束后你删除此牌名。<br>"..
  "当你造成或受到伤害后，若你手牌不为最多，你摸等同已删除牌名数的牌。",

  ["#ma__chuhai"] = "除害：将两张牌当一种“除害”牌使用，目标角色濒死结算后删除此牌名",

  ["$ma__chuhai1"] = "",
  ["$ma__chuhai2"] = "",
}

local chuhai_names={ "slash", "duel", "analeptic" }

ma__chuhai:addEffect("viewas", {
  name = "ma__chuhai",
  pattern = table.concat(chuhai_names, ","),
  interaction = function(self, player)
    local all_names, names = chuhai_names, {}
    for _, name in ipairs(player:getTableMark("@$ma__chuhai")) do
      local to_use = Fk:cloneCard(name)
      if ((Fk.currentResponsePattern == nil and player:canUse(to_use) and not player:prohibitUse(to_use)) or
            (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
        table.insert(names, name)
      end
    end
    if #names == 0 then return end
    return UI.CardNameBox { choices = names }
  end,
  card_num = 2,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function(self, player, cards)
    if #cards ~= 2 then return end
    if not self.interaction.data then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = ma__chuhai.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    player.room:setPlayerMark(player, "chuhai_name-record", use.card.trueName)
    use.unoffsetableList = table.simpleClone(room.players)
    use.extra_data = use.extra_data or {}
    use.extra_data.ma__chuhai = use.extra_data.ma__chuhai or {}
    use.extra_data.ma__chuhai[use.tos] = (use.extra_data.ma__chuhai[use.tos] or 0) + 1
  end,
  after_use = function(self, player, use)
    if player:getMark("chuhai_name-record") ~= 0 then
      player.room:setPlayerMark(player, "chuhai_name-record", 0)
    end
  end,
  enabled_at_play = function(self, player)
    for _, name in ipairs(player:getTableMark("@$ma__chuhai")) do
      local to_use = Fk:cloneCard(name)
      if player:canUse(to_use) and not player:prohibitUse(to_use) then
        return true
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    for _, name in ipairs(player:getTableMark("@$ma__chuhai")) do
      local to_use = Fk:cloneCard(name)
      if (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use)) then
        return true
      end
    end
  end,
})

ma__chuhai:addEffect(fk.AfterDying, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      if e then
        local use = e.data
        if use.extra_data and use.extra_data.ma__chuhai and use.extra_data.ma__chuhai[use.tos] then
          for _, to in ipairs(use.tos) do
            if to.id == target.id then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local card_name = player:getMark("chuhai_name-record")
    player:drawCards(2, ma__chuhai.name)
    room:removeTableMark(player, "@$ma__chuhai", card_name)
  end,
})

--[[ma__chuhai:addEffect(fk.AfterDying, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return data.extra_data and (data.extra_data.ma__chuhai or {})[player.id] and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card_name = player:getMark("chuhai_name-record")
    player:drawCards(2, ma__chuhai.name)
    room:removeTableMark(player, "@$ma__chuhai", card_name)
  end,
})

ma__chuhai:addEffect(fk.AfterCardTargetDeclared, {
  can_refresh = function (self, event, target, player, data)
    return (data.extra_data or {}).ma__chuhai == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    data.unoffsetableList = table.simpleClone(room.players)
  end,
})]]

ma__chuhai:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and table.contains(card.skillNames, ma__chuhai.name)
  end,
})

local spec = {
  anim_type = "drawCards",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ma__chuhai.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if not table.every(room.alive_players, function(p)
      return player:getHandcardNum() >= p:getHandcardNum()
    end) then 
      if not player.dead and #player:getTableMark("@$ma__chuhai") < 3 then
      return true end
    end
  end,
  on_use = function (self, event, target, player, data)
    local n = #chuhai_names - #player:getTableMark("@$ma__chuhai")
      player:drawCards(n, ma__chuhai.name)
  end,
}

ma__chuhai:addEffect(fk.Damage, spec)
ma__chuhai:addEffect(fk.Damaged, spec)

ma__chuhai:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "@$ma__chuhai", chuhai_names)
end)
ma__chuhai:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$ma__chuhai", 0)
end)

return ma__chuhai